package com.example.pkmedical.Activity.chairposition;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.pkmedical.Base.BaseActivity;
import com.example.pkmedical.Event.MessageWrap;
import com.example.pkmedical.R;
import com.example.pkmedical.Utils.CommandUtil;
import com.example.pkmedical.Utils.DistinguishUtil;
import com.example.pkmedical.Utils.FinishActivityManager;
import com.example.pkmedical.Utils.NumberUtil;
import com.example.pkmedical.Utils.SerialProtUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChairPositionOne extends BaseActivity {

    @BindView(R.id.chair_position_title)
    TextView chairPositionTitle;
    @BindView(R.id.chair_position_center_text)
    TextView chairPositionCenterText;
    @BindView(R.id.chair_position_progress)
    ProgressBar chairPositionProgress;
    @BindView(R.id.chair_top_layout)
    RelativeLayout chairTopLayout;
    @BindView(R.id.addok_cancle_button)
    Button addokCancleButton;
    @BindView(R.id.chair_top_layout_two)
    RelativeLayout chairTopLayoutTwo;
    @BindView(R.id.chair_state_img)
    ImageView chairStateImg;
    @BindView(R.id.chair_state_text)
    TextView chairStateText;
    @BindView(R.id.chair_position_back_lauout)
    LinearLayout chairPositionBackLauout;
    @BindView(R.id.chair_top_layout_three)
    RelativeLayout chairTopLayoutThree;
    @BindView(R.id.chair_top_layout_four)
    RelativeLayout chairTopLayoutFour;
    @BindView(R.id.chair_top_layout_five)
    RelativeLayout chairTopLayoutFive;
    @BindView(R.id.chair_position_progress_layout_out)
    RelativeLayout chairPositionProgressLayoutOut;
    private byte[] mBytes;
    private boolean isCanBack = true;//点击护栏其他按钮是否可以退出
    private boolean canListener = false;//是否开始监听按下和抬起

    //    private boolean mType = true;
//    private boolean canListener = false;
//    private boolean isClose = true;
//    private boolean isNext = false;
//    private boolean isCanBack = true;
//    private boolean canYellow = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chairposition_one);
        ButterKnife.bind(this);
        FinishActivityManager.getManager().addActivity(this);
    }

    @Override
    public void onReceiveEvent(MessageWrap event) {
        super.onReceiveEvent(event);
        if (event.getCode() == MessageWrap.CHAIR_POSITION) {
            mBytes = event.getBytes();
            distinguish(mBytes);
            Log.d("yangshuoyijian",NumberUtil.bytesToHexString(mBytes)+"");
        }
    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;
    }

    /**
     *
     */
    private void distinguish(byte[] bytes) {
        if (DistinguishUtil.distinguishUtil(bytes)) {
            Log.d("yangshuoYIJIANYIWEI",bytes[3]+"");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    switch (bytes[3]) {
                        case 0x01:
                            addokCancleButton.setVisibility(View.VISIBLE);
                            addokCancleButton.setText(getString(R.string.user_bt_cancle));
                            showone();
                            chairPositionCenterText.setVisibility(View.VISIBLE);
                            chairPositionCenterText.setText(getString(R.string.chair_pos_one_text));
                            chairPositionProgress.setProgress(2);
                            isCanBack = true;
                            canListener = false;
                            break;
                        case 0x02:
                            addokCancleButton.setVisibility(View.VISIBLE);
                            addokCancleButton.setText(getString(R.string.user_bt_cancle));
                            showone();
                            chairPositionCenterText.setVisibility(View.VISIBLE);
                            chairPositionCenterText.setText(getString(R.string.chair_pos_two_text));
                            chairPositionProgress.setProgress(4);
                            isCanBack = true;
                            canListener = false;
                            break;
                        case 0x03:
                            addokCancleButton.setText(getString(R.string.user_bt_cancle));
                            addokCancleButton.setVisibility(View.VISIBLE);
                            showtwo();
                            chairPositionCenterText.setText("");
                            chairStateImg.setImageResource(R.drawable.img_chair_three);
                            chairStateText.setText(getString(R.string.chair_pos_two_text_pieace));
                            chairPositionProgress.setProgress(4);
                            isCanBack = true;
                            canListener = false;
                            break;
                        case 0x04:
                            showtwo();
                            chairPositionCenterText.setVisibility(View.VISIBLE);
                            chairPositionCenterText.setText(getString(R.string.chair_pos_three_text));
                            chairStateImg.setImageResource(R.drawable.img_chair_four);
                            chairStateText.setText(getString(R.string.chair_pos_three_top_text));
                            chairPositionProgress.setProgress(6);
                            SerialProtUtil.getInstance().sendBytes(2, CommandUtil.HIP_LEG_VENTING);
                            addokCancleButton.setText(getString(R.string.user_bt_cancle));
                            addokCancleButton.setVisibility(View.VISIBLE);
                            canListener = false;
                            isCanBack = true;
                            break;
                        case 0x05:
                            showthree();
                            chairPositionBackLauout.setBackgroundColor(getResources().getColor(R.color.green_back));
                            chairPositionCenterText.setVisibility(View.GONE);
                            chairPositionProgress.setProgress(10);
                            addokCancleButton.setVisibility(View.GONE);
                            isCanBack = false;
                            canListener = true;
                            break;
//                        case 0x06:
//
//                            break;
                        case 0x20://臀部腿部放气完成
                            showtwo();
                            chairPositionCenterText.setVisibility(View.VISIBLE);
                            chairPositionCenterText.setText(getString(R.string.chair_pos_four_text));
                            chairStateImg.setImageResource(R.drawable.img_chair_three);
                            chairStateText.setText(getString(R.string.chair_pos_four_top_text));
                            chairPositionProgress.setProgress(6);
                            SerialProtUtil.getInstance().sendBytes(2, CommandUtil.HIP_LEG_VENTING_OK);
                            SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.REMIND_MESSAGE);
                            addokCancleButton.setVisibility(View.VISIBLE);
                            isCanBack = true;
                            canListener = false;
                            break;
                        case 0x21:
                            if (canListener) {
                                chairPositionProgressLayoutOut.setVisibility(View.VISIBLE);
                                switch (mBytes[4]) {
                                    case 0x01:
                                        showtwo();
                                        chairStateImg.setImageResource(R.drawable.img_chair_six);
                                        chairPositionBackLauout.setBackgroundColor(getResources().getColor(R.color.green_back));
                                        chairPositionCenterText.setVisibility(View.VISIBLE);
                                        chairPositionCenterText.setText(getString(R.string.chair_pos_six_text));
                                        chairStateText.setText(getString(R.string.chair_pos_six_top_text));
                                        chairPositionProgress.setProgress(10);
                                        addokCancleButton.setVisibility(View.GONE);
                                        SerialProtUtil.getInstance().sendBytes(2, CommandUtil.HOUBEI_ON);
                                        break;
                                    case 0x02:
                                        showthree();
                                        chairPositionBackLauout.setBackgroundColor(getResources().getColor(R.color.green_back));
                                        chairPositionCenterText.setVisibility(View.GONE);
                                        chairPositionProgress.setProgress(10);
                                        addokCancleButton.setVisibility(View.GONE);
                                        SerialProtUtil.getInstance().sendBytes(2, CommandUtil.HOUBEI_OFF);
                                        break;
                                }
                                isCanBack = false;
                            }
                            break;
                        case 0x22:
                            if (!isCanBack) {
                                showfour();
                                chairPositionProgressLayoutOut.setVisibility(View.GONE);
                                chairPositionCenterText.setVisibility(View.GONE);
                                addokCancleButton.setVisibility(View.GONE);
                                canListener = true;
                                isCanBack = false;
                            }
                            break;
                        case 0x23://11p
                            showfive();
                            chairPositionProgressLayoutOut.setVisibility(View.GONE);
                            chairPositionCenterText.setVisibility(View.GONE);
                            addokCancleButton.setVisibility(View.VISIBLE);
                            SerialProtUtil.getInstance().sendBytes(2, CommandUtil.CHANGGUI_TTYPE_CHAIR);
                            isCanBack = false;
                            canListener = false;
                            break;

                        case 0x24://常规完成，退出界面
                            SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.REMIND_MESSAGE);
                            ChairPositionOne.this.finish();
                            break;
                        case 0x25://
                            if (isCanBack) {
                                SerialProtUtil.getInstance().sendBytes50(2, CommandUtil.CHANGGUI_TTYPE);
                                SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.CHAIR_CANCLE);
                                ChairPositionOne.this.finish();
                            }
                            break;
                    }
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.CHAIR_0);
    }


    @Override
    public void initData() {

    }

    @Override
    public void initView() {

    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void showDialog(String... msg) {

    }

    @OnClick(R.id.addok_cancle_button)
    public void onViewClicked() {
        SerialProtUtil.getInstance().sendBytes50(2, CommandUtil.CHANGGUI_TTYPE);
        SerialProtUtil.getInstance().sendBytes200(2, CommandUtil.CHAIR_CANCLE);
        ChairPositionOne.this.finish();
    }

    private void showone() {
        chairPositionBackLauout.setBackgroundColor(getResources().getColor(R.color.white));
        chairTopLayout.setVisibility(View.VISIBLE);
        chairTopLayoutTwo.setVisibility(View.GONE);
        chairTopLayoutThree.setVisibility(View.GONE);
        chairTopLayoutFour.setVisibility(View.GONE);
        chairTopLayoutFive.setVisibility(View.GONE);
    }

    private void showtwo() {
        chairPositionBackLauout.setBackgroundColor(getResources().getColor(R.color.white));
        chairTopLayout.setVisibility(View.GONE);
        chairTopLayoutTwo.setVisibility(View.VISIBLE);
        chairTopLayoutThree.setVisibility(View.GONE);
        chairTopLayoutFour.setVisibility(View.GONE);
        chairTopLayoutFive.setVisibility(View.GONE);
    }

    private void showthree() {
        chairPositionBackLauout.setBackgroundColor(getResources().getColor(R.color.white));
        chairTopLayout.setVisibility(View.GONE);
        chairTopLayoutTwo.setVisibility(View.GONE);
        chairTopLayoutThree.setVisibility(View.VISIBLE);
        chairTopLayoutFour.setVisibility(View.GONE);
        chairTopLayoutFive.setVisibility(View.GONE);
    }

    private void showfour() {
        chairPositionBackLauout.setBackgroundColor(getResources().getColor(R.color.color_yellow));
        chairTopLayout.setVisibility(View.GONE);
        chairTopLayoutTwo.setVisibility(View.GONE);
        chairTopLayoutThree.setVisibility(View.GONE);
        chairTopLayoutFour.setVisibility(View.VISIBLE);
        chairTopLayoutFive.setVisibility(View.GONE);
    }

    private void showfive() {
        chairPositionBackLauout.setBackgroundColor(getResources().getColor(R.color.white));
        chairTopLayout.setVisibility(View.GONE);
        chairTopLayoutTwo.setVisibility(View.GONE);
        chairTopLayoutThree.setVisibility(View.GONE);
        chairTopLayoutFour.setVisibility(View.GONE);
        chairTopLayoutFive.setVisibility(View.VISIBLE);
    }
}
